package huixue.cn.com.library.observer;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;

import huixue.cn.com.library.callback.ICallBack;
import huixue.cn.com.library.entity.BaseHttpResult;
import huixue.cn.com.library.util.ToastUtils;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

/**
 * Created by gongdongyang on 2018/7/19.
 */

public abstract class BaseObserver<T> implements Observer<BaseHttpResult<T>>,ICallBack<T> {

    private final String TAG = BaseObserver.class.getSimpleName();

    private final int RESPONSE_CODE_OK = 0;      //自定义的业务逻辑，成功返回积极数据

    private final int RESPONSE_CODE_FAILED = -1; //返回数据失败

    private Disposable disposable;   //不处理吧

    private Context mContext;
    private boolean showProgress = true;

    public BaseObserver(Context mContext, boolean showProgress) {
        this.mContext = mContext;
        this.showProgress = showProgress;
        if(showProgress){
        }
    }

    public BaseObserver(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void onSubscribe(Disposable d) {
        disposable = d;
    }

    @Override
    public void onNext(BaseHttpResult<T> response) {
        ToastUtils.showToast("onNext"+response.getResCode());
        if (response.getResCode() == RESPONSE_CODE_OK) {
            onSuccess(response.getResContent());
        }else{
            onFailure(response.getResCode(), response.getResMsg());
        }
    }

    @Override
    public void onError(Throwable t) {
        int code = 0;
        String errorMessage = "未知错误";
        if (t instanceof HttpException) {
            HttpException httpException = (HttpException) t;
            String meg = httpException.response().toString();   //
            code = httpException.code();
            errorMessage = httpException.getMessage();
        } else if(t instanceof SocketTimeoutException){
            code = RESPONSE_CODE_FAILED;
            errorMessage = "服务器响应超时";
        } else if(t instanceof ConnectException){
            code = RESPONSE_CODE_FAILED;
            errorMessage = "网络连接异常，请检查网络";
        } else if (t instanceof RuntimeException) {
            code = RESPONSE_CODE_FAILED;
            errorMessage = "运行时错误";
        } else if(t instanceof UnknownHostException){
            code = RESPONSE_CODE_FAILED;
            errorMessage = "无法解析主机，请检查网络连接";
        } else if(t instanceof UnknownServiceException){
            code = RESPONSE_CODE_FAILED;
            errorMessage = "未知的服务器错误";
        } else if(t instanceof IOException){
            code = RESPONSE_CODE_FAILED;
            errorMessage = "没有网络，请检查网络连接";
        }

        /**
         * 严重的错误弹出dialog，一般的错误就只要Toast
         */
        if (RESPONSE_CODE_FAILED == code) {
            onFailure(RESPONSE_CODE_FAILED, errorMessage);
        } else {
            if (mContext != null && !((Activity) mContext).isFinishing()) {
                Toast.makeText(mContext, errorMessage + " - " + code, Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onComplete() {

    }

    /**
     * 网络请求失败
     * @param code
     * @param message
     */
    public void onFailure(int code, String message) {
        if (code == RESPONSE_CODE_FAILED && mContext != null) {

        } else {
            disposeEorCode(message, code);
        }
    }

    @Override
    public void onSuccess(T t) {

    }

    /**
     * 对通用问题的统一拦截处理
     *
     * @param code
     */
    private final void disposeEorCode(String message, int code) {
        switch (code) {
            case 101:
            case 112:
            case 123:
            case 401:
                //退回到登录页面

                break;
        }
        Toast.makeText(mContext, message + " # " + code, Toast.LENGTH_SHORT).show();
    }
}
